package com.momo.todo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.momo.todo.model.PlanModel;
import com.momo.todo.R;
import com.momo.todo.utils.LogUtils;
import com.momo.todo.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/24.
 */

public class PlanListAdapter extends BaseAdapter {

    private Context context;
    private List<PlanModel> planList = new ArrayList<>();
    private LayoutInflater inflater;

    public PlanListAdapter(Context context, ArrayList<PlanModel> planList) {
        this.context = context;
        this.planList = planList;
        inflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        if (planList == null) {
            return 0;
        } else {
            return planList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (planList == null) {
            return null;
        } else {
            return planList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public int getItemViewType(int position) {
        return planList.get(position).getType();
    }

    class ViewHolder {
        LinearLayout liner_plan;
        TextView tv_content;
        TextView tv_time;
        ImageView iv_top;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_plan_list, null);

            holder = new ViewHolder();
            holder.liner_plan = (LinearLayout) convertView.findViewById(R.id.liner_plan);
            holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            holder.iv_top = (ImageView) convertView.findViewById(R.id.iv_top);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        int type = getItemViewType(position);

        switch (type) {
            case PlanModel.PLAN_TYPE_STUDY:
                holder.liner_plan.setBackgroundResource(R.drawable.bg_plan_study);
                break;
            case PlanModel.PLAN_TYPE_LIFE:
                holder.liner_plan.setBackgroundResource(R.drawable.bg_plan_life);
                break;
            default:
                holder.liner_plan.setBackgroundResource(R.drawable.bg_plan_other);
                break;
        }

        String content = planList.get(position).getContent();

        String time = TimeUtils.timeStamp2Date(planList.get(position).getTime());
        holder.tv_time.setText(time);
        holder.tv_content.setText(content);


        if (planList.get(position).isTop()) {
//            LogUtils.log(planList.get(position).toString());
            holder.iv_top.setVisibility(View.VISIBLE);
            holder.iv_top.setBackgroundResource(R.drawable.top_icon);
        } else {
            holder.iv_top.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    public void refresh(List<PlanModel> planlist) {
        this.planList = planlist;
        notifyDataSetChanged();
    }
}











